package com.lc.user.express.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.Net;
import com.lc.user.express.model.HomeModel;

import java.util.List;


/**
 * Created by Administrator on 2016/3/1.
 */
public class HomeAdapter extends BaseListAdapter<HomeModel> {
    public HomeAdapter(Context context, List<HomeModel> data) {
        super(context, data);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        HomeModel homeModel = data.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_home_list, null);
            AppContext.ScaleScreenHelper.loadView((ViewGroup) convertView);
            viewHolder = new ViewHolder();
            viewHolder.iv_pic = (ImageView) convertView.findViewById(R.id.iv_pic);
            viewHolder.tv_car_name = (TextView) convertView.findViewById(R.id.tv_car_name);
            viewHolder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            viewHolder.tv_miles = (TextView) convertView.findViewById(R.id.tv_miles);
            viewHolder.tv_volum = (TextView) convertView.findViewById(R.id.tv_volum);
            viewHolder.tv_weight = (TextView) convertView.findViewById(R.id.tv_weight);
            viewHolder.tv_super_fee = (TextView) convertView.findViewById(R.id.tv_super_fee);
            viewHolder.tv_line = (TextView) convertView.findViewById(R.id.tv_line);
            convertView.setTag(viewHolder);
        }
        setContentView(convertView, homeModel);
        return convertView;
    }

    private void setContentView(View view, HomeModel homeModel) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        viewHolder.tv_car_name.setText(homeModel.getCarName());
        viewHolder.tv_money.setText(homeModel.getMoney());
        viewHolder.tv_miles.setText("元/" + homeModel.getMiles() + "公里");
        viewHolder.tv_weight.setText("载重：" + homeModel.getWeight() + "吨");
        viewHolder.tv_volum.setText("长*宽*高：" + homeModel.getVolum() + "m");
        viewHolder.tv_super_fee.setText("超公里费：" + homeModel.getSuperFee() + "元/公里");
        System.out.println(Net.SERVER + homeModel.getPicUrl());
        //   ImageManager.from(mContext).displayImage(viewHolder.iv_pic, Net.SERVER + homeModel.getPicUrl(), R.drawable.shape_background);
        Glide.with(mContext)
                .load(Net.SERVER + homeModel.getPicUrl())
                .placeholder(R.drawable.shape_background) // can also be a drawable
                .error(R.drawable.shape_background) // will be displayed if the image cannot be loaded
                .crossFade()
                .into(viewHolder.iv_pic);
    }

    private class ViewHolder {
        ImageView iv_pic;
        TextView tv_car_name;
        TextView tv_money;
        TextView tv_miles;
        TextView tv_volum;
        TextView tv_weight;
        TextView tv_super_fee;
        TextView tv_line;
    }
}
